package com.example.weatherforcast;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.Utils.DBUtil;
import com.example.adapter.MainPagerAdapter;
import com.example.adapter.MeAdapter;
import com.example.application.MyApplication;
import com.example.entity.Sk;
import com.example.entity.Today;
import com.example.entity.Weather;
import com.example.fragment.MainFragment;
import com.google.gson.Gson;

public class MainActivity extends FragmentActivity implements OnClickListener {
	private MainFragment mainFragment;
	private MainPagerAdapter mainPagerAdapter;
	private SharedPreferences mpreferences;
	private List<String> citylist;
	private List<Fragment> flist = null;
	FragmentManager manager;
	private MyApplication mapplication;
	TextView tv_me;
	Button btn_me, btn_city;
	Button btn_login, btn_register;
	ListView lv_login;
	ViewPager vg;
	DrawerLayout dl;
	LinearLayout dl_me, ll_main;
	MeAdapter meAdapter;
	SQLiteDatabase db = null;
	public static boolean flag = false, flagpager = true;

	String me[] = { "娱乐公告", "活动中心", "个性助手", "游戏中心", "查流量,充值" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		Intent intent = new Intent("firstactivity");
		startActivity(intent);

		initView();
		initData();
		adapter();
		listen();
	}

	private void initView() {
		// TODO Auto-generated method stub
		tv_me = (TextView) findViewById(R.id.tv_me);
		manager = getSupportFragmentManager();
		dl = (DrawerLayout) findViewById(R.id.dl);
		dl_me = (LinearLayout) findViewById(R.id.dl_me);
		ll_main = (LinearLayout) findViewById(R.id.ll_main);
		lv_login = (ListView) findViewById(R.id.lv_login);
		btn_me = (Button) findViewById(R.id.btn_me);
		btn_city = (Button) findViewById(R.id.btn_city);
		// btn_register = (Button) findViewById(R.id.btn_register);
		btn_login = (Button) findViewById(R.id.btn_login);
		vg = (ViewPager) findViewById(R.id.vg);

	}

	private void initData() {
		// TODO Auto-generated method stub
		mapplication = MyApplication.getInstance();
		db = mapplication.getDb();
		flist = new ArrayList<Fragment>();
		citylist = new ArrayList<String>();
		mpreferences = getSharedPreferences("city", MODE_PRIVATE);
		int count = mpreferences.getInt("count", -1);
		for (int i = 0; i < count; i++) {
			citylist.add(mpreferences.getString(i + 1 + "", "上海市"));
		}
		if (count > 0) {
			btn_city.setText(citylist.get(0));
			setback(citylist.get(0));
		}

		for (int i = 0; i < count; i++) {
			flist.add(MainFragment.newInstances(citylist.get(i)));

		}
	}

	private void adapter() {
		// TODO Auto-generated method stub
		mainPagerAdapter = new MainPagerAdapter(manager, flist);
		meAdapter = new MeAdapter(me, this);
		vg.setAdapter(mainPagerAdapter);
		lv_login.setAdapter(meAdapter);
		// flist.clear();
		// citylist.clear();
	}

	private void listen() {
		// TODO Auto-generated method stub
		btn_me.setOnClickListener(this);
		btn_city.setOnClickListener(this);
		btn_login.setOnClickListener(this);
		vg.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {

				for (int i = 0; i < citylist.size(); i++) {

					if (arg0 == i) {
						btn_city.setText(citylist.get(i));
						setback(citylist.get(i));
						// mainFragment.HttpGetData(citylist.get(i));
						// mainFragment.QueryData(citylist.get(i));
					}
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_me:
			dl.openDrawer(dl_me);
			break;
		case R.id.btn_city:
			Intent intent = new Intent(this, CityActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.opencity, R.anim.opensecond);
			break;
		case R.id.btn_login:
			Intent intent1 = new Intent(this, LoginActivity.class);
			startActivity(intent1);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (flag) {
			int count = mpreferences.getInt("count", -1);
			flist.clear();
			citylist.clear();
			for (int i = 1; i < count + 1; i++) {
				citylist.add(mpreferences.getString(i + "", "上海市"));
			}
			for (int i = 0; i < count; i++) {
				flist.add(MainFragment.newInstances(citylist.get(i)));

			}

			mainPagerAdapter.notifyDataSetChanged();

			int num = vg.getCurrentItem();
			if (citylist.size() > 0) {
				btn_city.setText(citylist.get(num));
				setback(citylist.get(num));
			}

		}
		SharedPreferences sp = getSharedPreferences("data", this.MODE_PRIVATE);
		if (sp.getBoolean("denglu", false)) {
			tv_me.setText("你的专属音乐，祝你幸福");
			Editor editor = sp.edit();
			editor.putBoolean("denglu", false);
			editor.commit();
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		flag = true;
	}

	private void setback(String city) {
		String weather = null;
		int time = 0;
		Today today = null;
		Sk sk = null;
		String data = DBUtil.querySingle(db, city);

		if (data != null) {
			Gson gson = new Gson();
			Weather w = gson.fromJson(data, Weather.class);
			today = w.getResult().getToday();
			sk = w.getResult().getSk();
		}
		if (sk != null) {
			weather = today.getWeather();
			String st = sk.getTime();
			if (weather.contains("转")) {
				weather = weather.substring(0, weather.indexOf("转"));
			}
			st = st.substring(0, 2);
			time = Integer.parseInt(st);
			if (weather.equals("晴") && (7 < time) && (time < 19)) {
				ll_main.setBackgroundResource(R.drawable.bg_sunny);
			}
			if (weather.equals("晴") && ((8 > time) || (time > 18))) {
				ll_main.setBackgroundResource(R.drawable.bg_ye);
			}
			if (weather.equals("多云") && (7 < time) && (time < 19)) {
				ll_main.setBackgroundResource(R.drawable.bg_duoyun);
			}
			if (weather.equals("多云") && ((8 > time) || (time > 18))) {
				ll_main.setBackgroundResource(R.drawable.bg_ye);
			}
			if (weather.contains("雨")) {
				ll_main.setBackgroundResource(R.drawable.bg_yu);
			}
			if (weather.contains("雾")) {
				ll_main.setBackgroundResource(R.drawable.bg_fog_and_haze);
			}
			if (weather.contains("雪")) {
				ll_main.setBackgroundResource(R.drawable.bg_xue);
			}
			if (weather.equals("阴") ) {
				ll_main.setBackgroundResource(R.drawable.yin);
			}

		}
	}
}
